function Agreement_Code() {
	var buffer = new StringBuffer();
	buffer.append('<div id="find" class="finds"><div class="searchinput ui-border-radius"><input id="keyword" class="bordersolid" type="text" value="" onblur="Show_Search()" onkeyup="Agreement_Search();"/></div><div id="searchicon" class="searchicon"><img src="img/jing.jpg"/></div><div id="searchicons" class="searchicons"><img src="img/jings.jpg"/></div><div class="screen"><button id="screen" class="ui-border-radius">筛选</button></div></div>');
	buffer.append('<div class="horizen border-bottom"></div>');
	return buffer.toString();
};
function Agreement_Data(data) {
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
		buffer.append('<div class="matelist border-bottom" id="'+info.ID+'"><div class="matename"><div class="realname">'+info.RealName+'</div><div class="birthplace">'+info.BirthPlace+'</div></div><div class="mateinfo"><div class="sex">'+info.Sex+'&nbsp;('+DateDiff("Y", info.Birthday, now+'')+'岁)</div><div class="joindate agreementdate">合同日期：<font>'+info.AgreeMentDate.ToDates()+'</font></div></div></div>');
	});
	return buffer.toString();
};
function Init_Agreement_Code()
{
	get("find").addEventListener("tap",function(){
		$("#searchicon").hide();
	    $("#searchicons").show();
	    $("#keyword").attr("placeholder","姓名、用户名、电话");
		if($("keyword").val()!="")
		{
			//WorkMate_Search();
		}
	});
	get("screen").addEventListener("tap",function(){
		OpenScreen(User_Search_Code(),function(){
			Init_User_Search();
		},Agreement_Search);
		stopEvent();
	});
};
function Agreement_Search(){
	pageindex=1;
	$("#divmsg1").attr("pageindex","0");
	Receive_Agreement(1);
};
function Receive_Agreement(pageindex)
{
	var isenable=RUndefined($("input[name='state_search']:checked").val());
	var departid=RUndefined($("#depart_search").val());
	var data = {};
			data.act = "Find_User";
			data.userid=userid;
			data.comid=comid;
			data.pageindex=pageindex;
			data.source=source;
			data.pagesize=PAGE_SIZE20;
			data.keyword = $("#keyword").val();
			data.timer = RUndefined($("#begindate_search").val()) + "～" + RUndefined($("#enddate_search").val());
		    data.isenable = isenable==""?"true":isenable;
    		data.departid = departid==""?"0":departid;
    		data.sign=GetSign(data.act);
			
			mui.getJSON(admin_domain, data, function(json) {
				if(json.data.user.length>0)
				{
					if(pageindex==1)
					{
						$("#contentlist1").html(Agreement_Data(json.data.user));
					}else{
						$("#contentlist1").append(Agreement_Data(json.data.user));
					}
					setTimeout(function(){
						Init_Agreement();
						myScroll1.refresh();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.user.length!=data.pagesize)
					{
						$("#loading1").html("");
					}else{
						$("#loading1").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					
					if(pageindex==1)
					{
						$("#contentlist1").html("");
						$("#loading1").html("主人，没有找到您需要的数据"); 
					}
					else
					{
						$("#loading1").html("没有更多了");
					}
				}
				
         		pageindex = parseInt($("#divmsg1").attr("pageindex"))+1;
         		$("#divmsg1").attr("pageindex",pageindex);
			});
};
function Init_Agreement()
{
	mui("#contentlist1").off("tap",".matelist");
	mui("#contentlist1").on("tap",".matelist",function(){
		obj=$(this);
		OpenWindow(2,obj.find(".realname").html()+"的合同列表","新增",function(){
			OpenWindow(3,"新增合同","保存",function(){
				Save_Agreement();
			});
			var date=new Date();
			var year=date.getFullYear()+1;
			var month=date.getMonth()+1;
			var day=date.getDate();
			var nearyear=(year+"-"+month+"-"+day).ToDate();
			$("#contentlist3").html(AgreementAdd_Code(0,obj.find(".realname").html(),date.Format("yyyy-MM-dd"),nearyear,""));
			Init_AgreementAdd();
		});
		AgreementDetail_Data();
	});
	
};
function AgreementDetail_Data(){
	
	var data = {};
		data.act = "Find_Agreement";
		data.uid=obj.attr("id");
		data.source=source;
		data.userid=userid;
		data.comid=comid;
		data.sign=GetSign(data.act);
		
		mui.getJSON(index_domain, data, function (json) {
			var str="";
		    if (json.state) {
		    	if(json.data.list.length>0)
		    	{
			    	$.each(json.data.list,function(i,info){
			    		str+='<div class="matelist border-bottom" id="'+info.ID+'" bak="'+info.Bak+'"><div class="matename"><div class="realname">'+obj.find(".realname").html()+'</div><div class="birthplace"><font></font></div></div><div class="mateinfo"><div class="sex begindate">开始日期：<font>'+info.BeginDate.ToDate()+'</font></div><div class="joindate enddate">结束日期：<font>'+info.EndDate.ToDate()+'</font></div></div></div>';
			    	});
			    	$("#contentlist2").html(str);
		    	}else
		    	{
		    		$("#contentlist2").html('<div id="loading2" class="loading">主人，没有找到相关合同记录</div>');
		    	}
		    }
		    if($("#contentlist2 .matelist").length>0){
			    $("#contentlist2 .matelist:last")[0].addEventListener("tap",function(){
			    	OpenWindow(3,"修改合同","保存",function(){
						Save_Agreement();
					});
					$("#contentlist3").html(AgreementAdd_Code($(this).attr("id"),obj.find(".realname").html(),$(this).find(".begindate font").html(),$(this).find(".enddate font").html(),$(this).attr("bak")));
					Init_AgreementAdd();
			    });
		    }
		});
		
};
function AgreementAdd_Code(id,realname,begindate,enddate,bak){
	var buffer = new StringBuffer();
	buffer.append('<div id="agreementid" val="'+id+'" class="crmsheet border-bottom"><div class="ctitle"><div class="cname">员工姓名</div></div><div class="ctext"><input type="text" readonly="readonly" value="'+realname+'" /></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom date"><div class="ctitle"><div class="cname">开始日期</div><div class="require">*</div></div><div class="ctext"><input id="txt_begindate" type="text" readonly="readonly" placeholder="选择合同开始日期" value="'+begindate+'" /></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom date"><div class="ctitle"><div class="cname">结束日期</div><div class="require">*</div></div><div class="ctext"><input id="txt_enddate" type="text" readonly="readonly" placeholder="选择合同结束日期" value="'+enddate+'" /></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">备注</div></div><div class="ctext"><textarea id="txt_bak" placeholder="合同备注"  style="height:120px;">'+bak+'</textarea></div><div class="carrow"></div></div>');
	if(id!=0){
	buffer.append('<div id="deletesheet" class="deletesheet" style="margin-top:50px;"><div class="name" style="width:100%; text-align:center;">删除该合同</div></div>');
	}
	return buffer.toString();
};


function Init_AgreementAdd(){
	mui(".divmsg").on("tap",".date",function(){
			obj10=this;
			var t=new Date();
			
			if($(this).find("input").val()!="")
			{
				var year_month_day=$(this).find("input").val().split("-");
				t.setFullYear(year_month_day[0]);
				t.setMonth(year_month_day[1]-1);
				t.setDate(year_month_day[2]);
			}
			plus.nativeUI.pickDate( function(e){
				var d=e.date;
				$(obj10).find("input").val((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).ToDate());
			},function(e){
				console.log( "未选择日期："+e.message );
			},{date:t});
	});
	if($("#agreementid").attr("val")!="0")
		{
			get("deletesheet").addEventListener("tap",function(){
		        	plus.nativeUI.confirm("确定要删除吗？", function(event){
						if(event.index==1)
						{
							var data = {};
						    data.act = "Delete_Agreement";
						    data.ids = $("#agreementid").attr("val");
						    data.uid=obj.attr("id");
						    data.userid=userid;
							data.comid=comid;
							data.source=source;
							data.sign=GetSign(data.act);
						    mui.getJSON(index_domain, data, function(json) {
								if(json.state)
								{
									$("#contentlist2 .matelist:last").remove();
									if($("#contentlist2 .matelist").length==0)
									{
										obj.find(".agreementdate font").html("暂无");
									}else
									{
										obj.find(".agreementdate font").html($("#contentlist2 .matelist:last").find(".enddate font").html());
									}
									AgreementDetail_Data();
									CloseWindow();
									plus.nativeUI.toast(json.msg);
								}else
								{
									plus.nativeUI.alert(json.msg);
								}
								
						    });	
						}
					},"提示", ["取消","确认"]);
				
			});
		}
	
};


function Save_Agreement() {
        var data = {};
        data.id = $("#agreementid").attr("val");
		data.act = "Save_Agreement";
		data.uid=obj.attr("id");
		data.begindate = $("#txt_begindate").val();
        data.enddate = $("#txt_enddate").val();
        data.bak=$("#txt_bak").val();
        data.userid=userid;
		data.comid=comid;
        data.source = source;
        data.version = version;
        data.sign=GetSign(data.act);
        plus.nativeUI.showWaiting( "正在执行..." );
	    CloseWaiting(10000);
        mui.getJSON(index_domain, data, function (json) {
        	plus.nativeUI.closeWaiting();
        	plus.nativeUI.toast(json.msg);
            if (json.state) {
                CloseWindow(); 
                obj.find(".agreementdate font").html(data.enddate);
                AgreementDetail_Data();
            }
        });
};
